import hashlib
from django import forms
from django.core.exceptions import ValidationError
from rbac.models import UserInfo
from app_main import models
from stark.widget.my_time import MyDateInput


class MyBaseModelForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for name, f in self.fields.items():  # 添加样式
            f.widget.attrs['class'] = 'form-control'


class UserInfoAddModelForm(MyBaseModelForm):
    confirm_pwd = forms.CharField(label='确认密码', max_length=64)

    class Meta:
        model = UserInfo
        fields = ['name', 'password', 'confirm_pwd', 'email', 'nickname', 'phone', 'gender', 'roles', 'department']

    def clean_confirm_pwd(self):  # 检测密码是否一致
        password = self.cleaned_data['password']
        confirm_pwd = self.cleaned_data['confirm_pwd']
        if password != confirm_pwd:
            raise ValidationError('两次密码输入不一致')

        return confirm_pwd

    def clean(self):
        pwd = self.cleaned_data['password']
        md = hashlib.md5('sal'.encode('utf-8'))
        md.update(pwd.encode('utf-8'))
        self.cleaned_data['password'] = md.hexdigest()
        return self.cleaned_data


class UserInfoEditModelForm(MyBaseModelForm):

    class Meta:
        model = UserInfo
        fields = ['name', 'email', 'nickname', 'phone', 'gender', 'roles', 'department']


class ResetPasswordForm(MyBaseModelForm):
    password = forms.CharField(label='新密码', max_length=64, widget=forms.PasswordInput)
    confirm_password = forms.CharField(label='确认新密码', max_length=64, widget=forms.PasswordInput)

    def clean_confirm_password(self):  # 检测密码是否一致
        password = self.cleaned_data['password']
        confirm_pwd = self.cleaned_data['confirm_password']
        if password != confirm_pwd:
            raise ValidationError('两次密码输入不一致')

        return confirm_pwd

    def clean(self):
        pwd = self.cleaned_data['password']
        md = hashlib.md5('sal'.encode('utf-8'))
        md.update(pwd.encode('utf-8'))
        self.cleaned_data['password'] = md.hexdigest()
        return self.cleaned_data


class ClassListModelForm(MyBaseModelForm):

    class Meta:
        model = models.ClassList
        fields = '__all__'
        widgets = {
            'start_date': MyDateInput,
            'graduate_date': MyDateInput
        }


class CustomerPublicModelForm(MyBaseModelForm):
    class Meta:
        model = models.Customer
        exclude = ['consultant']


class CustomerPrivateModelForm(MyBaseModelForm):
    class Meta:
        model = models.Customer
        exclude = ['consultant']


class ConsultRecordModelForm(MyBaseModelForm):
    class Meta:
        model = models.FollowRecord
        fields = ['note']


class PayRecordModelForm(MyBaseModelForm):
    class Meta:
        model = models.PaymentRecord
        fields = ['pay_type', 'money', 'to_class', 'note']


class MorePayRecordModelForm(MyBaseModelForm):
    qq = forms.CharField(label='QQ', max_length=32)
    mobile = forms.CharField(label='手机号', max_length=32)
    emergency_contact = forms.CharField(label='紧急联系人电话', max_length=32)

    class Meta:
        model = models.PaymentRecord
        fields = ['pay_type', 'money', 'to_class', 'qq', 'mobile', 'emergency_contact', 'note']


class StudentModelForm(MyBaseModelForm):
    class Meta:
        model = models.Student
        fields = ['qq', 'mobile', 'emergency_contact', 'memo']


class ScoreRecordModelForm(MyBaseModelForm):
    class Meta:
        model = models.ScoreRecord
        fields = ['content', 'score_change']


class TeachRecordModelForm(MyBaseModelForm):
    class Meta:
        model = models.TeachRecord
        fields = ['day_num', 'teacher']


class StudyRecordModelForm(MyBaseModelForm):
    class Meta:
        model = models.StudyRecord
        fields = ['record']
